/*
 *  Copyright (C) 2009 takaji
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * AccountPeriodDetail.java
 *
 * Created on Dec 27, 2009, 4:13:51 PM
 */
package dakside.hacc.modules.accounting.periodman;

import org.dakside.exceptions.ArgumentException;
import dakside.hacc.core.models.AccountPeriod;
import org.dakside.utils.DateTimeHelper;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Takaji
 */
public class AccountPeriodDetails extends javax.swing.JPanel {

    private AccountPeriod accountPeriod = null;

    /** Creates new form AccountPeriodDetail */
    public AccountPeriodDetails() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtStatus = new javax.swing.JTextField();
        txtFromDate = new com.toedter.calendar.JDateChooser();
        txtToDate = new com.toedter.calendar.JDateChooser();

        jLabel3.setText("Title");
        jLabel3.setName("jLabel3"); // NOI18N

        txtTitle.setText("Unknown");
        txtTitle.setName("txtTitle"); // NOI18N

        jLabel1.setText("From Date");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("To Date");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText("Status");
        jLabel4.setName("jLabel4"); // NOI18N

        txtStatus.setName("txtStatus"); // NOI18N

        txtFromDate.setName("txtFromDate"); // NOI18N

        txtToDate.setName("txtToDate"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                    .addComponent(txtFromDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                    .addComponent(txtToDate, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                    .addComponent(txtStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtFromDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtToDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private com.toedter.calendar.JDateChooser txtFromDate;
    private javax.swing.JTextField txtStatus;
    private javax.swing.JTextField txtTitle;
    private com.toedter.calendar.JDateChooser txtToDate;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the accountPeriod
     */
    public AccountPeriod getAccountPeriod() {
        if (accountPeriod == null) {
            accountPeriod = new AccountPeriod();
        }

        accountPeriod.setTitle(txtTitle.getText());
        try {
            //clear from date - to date
            Date fromDate = txtFromDate.getDate();
            Date toDate = txtToDate.getDate();
            if (fromDate != null && toDate != null && fromDate.after(toDate)) {
                JOptionPane.showMessageDialog(null, "Invalid from date - to date", "Error", JOptionPane.ERROR_MESSAGE);
                return null;
            }
            accountPeriod.setFromDate(fromDate);
            accountPeriod.setToDate(toDate);
        } catch (ArgumentException ex) {
            Logger.getLogger(AccountPeriodDetails.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }


        return accountPeriod;
    }

    /**
     * @param accountPeriod the accountPeriod to set
     */
    public void setAccountPeriod(AccountPeriod accountPeriod) {
        this.accountPeriod = accountPeriod;
        //display model
        txtTitle.setText(accountPeriod.getTitle());
        //from date
        txtFromDate.setDate(accountPeriod.getFromDate());
        //to date
        txtToDate.setDate(accountPeriod.getToDate());
        //status
        txtStatus.setText(accountPeriod.getStatusString());
    }

    void createNew() {
        try {
            setAccountPeriod(new AccountPeriod("Untitled Account Period", new Date(), new Date()));
        } catch (ArgumentException ex) {
            Logger.getLogger(AccountPeriodDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
